<?php

/**
 * uniBoard ACP New Icon
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-15 17:35:38 +0000 (Wed, 15 Sep 2010) $
 * $Revision: 61 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_new_icon{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * Our api
	 *
	 * @var object
	 */
	
	private $api			= NULL;
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Create icon api
		$this -> api = new api_icon( $appCore);
			
		// Update?
		if ( $appCore -> _POST( 'update') == 1)
		{
			// Fill Api
			$this -> api -> setIconImage( $appCore -> _POST( 'icon_image'));
			
			// Get Image
			$icon_image = $appCore -> _POST( 'icon_image');
			
			// Image has extension?
			if ( strpos( $icon_image, '.') !== false)
			{
				// Grab extension
				$icon_extension = substr( $icon_image, strrpos( $icon_image, '.'));
				
				// Grab file name
				$icon_image = $appCore -> secureFilename( substr( $icon_image, 0, strrpos( $icon_image, '.'))) . $icon_extension;
				
				// Set file name
				$this -> api -> setIconImage( $icon_image);
				
				// File incorrect?
				if ( !file_exists( TOPICS_ICONS_PATH . $icon_image) || !( strtolower( $icon_extension) == '.gif' || strtolower( $icon_extension) == '.png' || strtolower( $icon_extension) == '.jpg' || strtolower( $icon_extension) == '.jpeg'))
				{
					// Image incorrect
					$this -> api -> setError( $appCore -> lang['acp_icon_image_does_not_exsist']);
				}
				else
				{
					// Detect image size
					$image_size = getimagesize( TOPICS_ICONS_PATH . $icon_image);
					
					// Put sizes into api
					$this -> api -> setIconWidth( $image_size[0]);
					$this -> api -> setIconHeight( $image_size[1]);
				}
			}
			else
			{
				// Image incorrect
				$this -> api -> setError( $appCore -> lang['acp_icon_image_does_not_exsist']);
			}
			
			// Errors?
			if ( $this -> api -> error)
			{
				// Draw Errors
				$appCore -> output -> error( join( '<br />', $this -> api -> errors), $appCore -> lang['new_icon_form']);
				
				// Repeat Form
				$this -> drawForm();
			}
			else
			{
				// Save api
				$this -> api -> saveData();
				
				// Set Log
				$appCore -> logAdmin( 'log_admin_created_icon', htmlspecialchars( $this -> api -> icon_image));
				
				// Redirect back to smileys page
				$appCore -> output -> drawRedirect( $appCore -> adminLink( 'act=icons'), $appCore -> lang['acp_icon_new_added']);
			}
		}
		else
		{
			// Draw Form
			$this -> drawForm();
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws Form
	 *
	 */
	
	function drawForm(){
	
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
				
		// Cache output
		ob_start();
				
		// Open Form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=new_icon'));
		echo $this -> appCore -> drawHidden( 'update', 1);
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['icon_form_sect_image']);
		
		// Draw file selection
		if ( USE_GLOB)
		{
			// Make array with images
			$icon_images = glob( TOPICS_ICONS_PATH . '*.{gif,png,jpg,jpeg}', GLOB_BRACE);
			
			// Drawing data
			$row = '';
			$row_images = 0;
			
			// Anything in array?
			if ( empty( $icon_images))
			{
				// Draw row
				echo $this -> appCore -> output -> tableRow( $this -> appCore -> lang['acp_icon_images_not_found']);
			}
			else
			{
				// Reiterate array
				foreach ( $icon_images as $image)
				{
					// Image val
					$image_file = substr( $image, strrpos( $image, '/') + 1);
					
					// Image sizes
					$image_sizes = getimagesize( $image);
					
					// Draw item
					$row .= '<div class="cell' . ( empty( $row_images) ? '' : ' left_border') . ' left_float width_quarter_s center">
				<label for="select_' . htmlspecialchars( $image_file) . '"><img src="' . $image . '" width="' . $image_sizes[0] . '" height="' . $image_sizes[1] . '" alt="" title="" /></label>
				<div>' . $this -> appCore -> drawRadio( 'icon_image', htmlspecialchars( $image_file), false, 'select_' . htmlspecialchars( $image_file)) . '</div>
			</div>';
					
					// Increase stat
					$row_images ++;
					
					// Draw row?
					if ( $row_images == 4)
					{	
						// Draw Row
						echo $this -> appCore -> output -> tableRow( $row);
						
						// Reset counter
						$row = '';
						$row_images = 0;
					}
				}
				
				// Draw last row?
				if ( $row_images > 0)
				{
					echo $this -> appCore -> output -> tableRow( $row);
					
				}
			}
		}
		else
		{
			echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['icon_form_file'], $this -> appCore -> drawTextInput( 'icon_image', $this -> api -> icon_image), sprintf( $this -> appCore -> lang['icon_form_file_expl'], TOPICS_ICONS_PATH));
		}
		
		// Close Form
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['new_icon_form_submit']);
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> closeForm();
		
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['new_icon_form'], array(
			$this -> appCore -> adminLink( 'act=icons') => $this -> appCore -> lang['acp_topic_icons_list'],
			'' => $this -> appCore -> lang['new_icon_form']
		), ob_get_clean());
			
	}

}